package cn.itcast.mq.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class SpringRabbitListener {

    /**
     * @description： SpringAMQP消费者三种消息确认模式 -- manual：手动确认
     * @date：2024/3/24  15:24
     * @return：
     **/
    //@RabbitListener(queues = "simple.queue")
    //public void listenSimpleQueue(String msg, Channel channel, Message message) throws IOException {
    //
    //    try {
    //        log.debug("消费者接收到simple.queue的消息：【" + msg + "】");
    //        System.out.println(1 / 0);
    //        //参数一：获取消息唯一标识  参数二：是否批量应答
    //        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    //    } catch (Exception e) {
    //        log.error("消费消息失败！！{}",e.getMessage());
    //        //参数三：是否将消息重新放回队列
    //        channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
    //    }
    //}

    /**
     * @description： SpringAMQP消费者三种消息确认模式 -- auto：默认  消费者出现异常，消息会放回队列，队列再投递给消费者
     * @date：2024/3/24  15:27
     * @return：
     **/
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue2(String msg, Channel channel, Message message) throws IOException {

            log.debug("消费者接收到simple.queue的消息：【" + msg + "】");
            System.out.println(1 / 0);

    }


    /**
     * @description： 监听死信队列消息
     * @date：2024/3/24  17:42
     * @return：
     **/
    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = "dl.queue", durable = "true"),
            exchange = @Exchange(name = "dl.direct"),
            key = "dl")
    )
    public void listenDlQueue(String msg) {
        log.info("接收到 dl.queue的延迟消息：{}", msg);
    }

    /**
     * @description： 使用延迟插件
     * @date：2024/3/24  19:17
     * @return：
     **/
    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = "delay.queue"),
            exchange = @Exchange(name = "delay.direct",delayed = "true"),
            key = "delay")
    )
    public void listenDelayQueue(String msg) {
        log.info("接收到 delay.queue的延迟消息：{}", msg);
    }



}
